<template>
  <el-dialog
    :title="!cover.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="cover" :rules="dataRule" ref="cover" @keyup.enter.native="coverSubmit()" label-width="80px">
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="cover.userId" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="详情地址" prop="address">
      <el-input v-model="cover.address" placeholder="详情地址"></el-input>
    </el-form-item>
    <el-form-item label="意向价格" prop="intenPrice">
      <el-input v-model="cover.intenPrice" placeholder="意向价格"></el-input>
    </el-form-item>
    <el-form-item label="意向区域" prop="intenArea">
      <el-input v-model="cover.intenArea" placeholder="意向区域"></el-input>
    </el-form-item>
    <el-form-item label="房产类型" prop="category">
      <el-input v-model="cover.category" placeholder="房产类型"></el-input>
    </el-form-item>
    <el-form-item label="求房类型" prop="type">
      <el-input v-model="cover.type" placeholder="求房类型"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createdAt">
      <el-input v-model="cover.createdAt" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="删除时间" prop="deletedAt">
      <el-input v-model="cover.deletedAt" placeholder="删除时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updatedAt">
      <el-input v-model="cover.updatedAt" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="coverSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        cover: {
          id: 0,
          userId: '',
          address: '',
          intenPrice: '',
          intenArea: '',
          category: '',
          type: '',
          createdAt: '',
          deletedAt: '',
          updatedAt: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '详情地址不能为空', trigger: 'blur' }
          ],
          intenPrice: [
            { required: true, message: '意向价格不能为空', trigger: 'blur' }
          ],
          intenArea: [
            { required: true, message: '意向区域不能为空', trigger: 'blur' }
          ],
          category: [
            { required: true, message: '房产类型不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '求房类型不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          deletedAt: [
            { required: true, message: '删除时间不能为空', trigger: 'blur' }
          ],
          updatedAt: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.cover.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['cover'].resetFields()
          if (this.cover.id) {
            this.$http({
              url: this.$http.adornUrl(`/cover/cover/info/${this.cover.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.cover.userId = data.cover.userId
                this.cover.address = data.cover.address
                this.cover.intenPrice = data.cover.intenPrice
                this.cover.intenArea = data.cover.intenArea
                this.cover.category = data.cover.category
                this.cover.type = data.cover.type
                this.cover.createdAt = data.cover.createdAt
                this.cover.deletedAt = data.cover.deletedAt
                this.cover.updatedAt = data.cover.updatedAt
              }
            })
          }
        })
      },
      // 表单提交
      coverSubmit () {
        this.$refs['cover'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cover/cover/${!this.cover.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.cover.id || undefined,
                'userId': this.cover.userId,
                'address': this.cover.address,
                'intenPrice': this.cover.intenPrice,
                'intenArea': this.cover.intenArea,
                'category': this.cover.category,
                'type': this.cover.type,
                'createdAt': this.cover.createdAt,
                'deletedAt': this.cover.deletedAt,
                'updatedAt': this.cover.updatedAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
